package com.softstao.guoyu.ui.activity;

import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;

import com.softstao.guoyu.R;
import com.softstao.guoyu.base.BaseActivity;
import com.softstao.guoyu.mvp.interactor.ForgetPassInteractor;
import com.softstao.guoyu.mvp.presenter.ForgetPassPresenter;
import com.softstao.guoyu.mvp.viewer.ChangePassViewer;
import com.softstao.softstaolibrary.library.wechatpay.MD5Util;
import com.softstao.softstaolibrary.library.widget.LZToast;
import com.wangjie.androidinject.annotation.annotations.mvp.AIPresenter;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by vaikus on 2017/6/24.
 */

public class SetPasswordActivity extends BaseActivity implements ChangePassViewer {

    @AIPresenter(presenter = ForgetPassPresenter.class, interactor = ForgetPassInteractor.class)
    ForgetPassPresenter presenter;

    @BindView(R.id.psd)
    EditText psd;
    @BindView(R.id.show_password)
    ImageButton showPassword;
    @BindView(R.id.confirm_psd)
    EditText confirmPsd;
    @BindView(R.id.loading)
    RelativeLayout loading;
    private String mobile, code;

    @Override
    public int _ContentView() {
        return R.layout.activity_set_password;
    }

    @Override
    public void initView() {
        initTitle("设置密码");
        titleBar.getDivider().setVisibility(View.GONE);
        mobile = getIntent().getStringExtra("mobile");
        code = getIntent().getStringExtra("code");
    }

    @OnClick({R.id.show_password, R.id.submit_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.show_password:
                if (view.isSelected()) {
                    psd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    confirmPsd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    showPassword.setSelected(false);
                } else {
                    psd.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                    confirmPsd.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                    showPassword.setSelected(true);
                }
                break;
            case R.id.submit_btn:
                if (!check()) {
                    return;
                }
                changePsd();
                break;
        }
    }

    private boolean check() {
        if (TextUtils.isEmpty(psd.getText())) {
            LZToast.getInstance(context).showToast("密码不能为空");
            return false;
        }
        if (psd.getText().length() < 6) {
            LZToast.getInstance(context).showToast("至少6位字符或数字");
            return false;
        }
        if (psd.getText().length() > 18) {
            LZToast.getInstance(context).showToast("不能超过18位字符或数字");
            return false;
        }
        if (TextUtils.isEmpty(confirmPsd.getText())) {
            LZToast.getInstance(context).showToast("确认密码不能为空");
            return false;
        }
        if (TextUtils.equals(psd.getText(), confirmPsd.getText()) == false) {
            LZToast.getInstance(context).showToast("输入密码不一致");
            return false;
        }
        return true;
    }

    @Override
    public void changePsd() {
        loading.setVisibility(View.VISIBLE);
        presenter.changePsd(mobile, code, MD5Util.MD5Encode(psd.getText().toString(), null), MD5Util.MD5Encode(confirmPsd.getText().toString(), null));
    }

    @Override
    public void changeResult(Object result) {
        loading.setVisibility(View.GONE);
        LZToast.getInstance(context).showToast("密码设置成功");
        finish();
    }

    @Override
    protected void error(String msg) {
        super.error(msg);
        loading.setVisibility(View.GONE);
    }
}
